<?php

namespace app\beautiful\controller\admin\partner;

use app\base\model\member\MemberCouponModel;
use app\BaseController;
use app\beautiful\model\mall\FlashSaleGoodsModel;
use app\beautiful\model\mall\PinTuanGoodsModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\partner\BonusModel;
use app\beautiful\model\partner\MemberConsumeBonusModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\partner\PartnerShareModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\ConsumeBonusModel;
use app\beautiful\model\setting\ConsumeBonusSettingModel;
use app\beautiful\model\setting\PartnerBonusSettingModel;
use app\beautiful\model\setting\PartnerDividendSettingModel;
use app\beautiful\model\setting\PartnerTaskBonusSettingModel;
use app\common\logic\DataLogic;
use app\common\model\MemberConsumeModel;
use app\common\model\OrderModel;
use app\stores\model\member\MemberLevelModel;
use app\stores\model\order\BuyOrderModel;


class ConsumeBonus extends BaseController{
    /*进行中*/
    public function index()

    {
        $page = $this->request->page;
        $limit = $this->request->limit;
        $search = $this->request->param('search');
        $time_types = $this->request->param('time_types/d',1);

        //消费分红
        $mc_model = new MemberConsumeModel();
        if(null === $consume_bonus_arr = $mc_model->getTime(['shop_id'=>$this->request->shopId,'type'=>$time_types,'status'=>1])){
            $return['bonus'] = [];
            return $this->success($return);
        }
        $consume_bonus = $consume_bonus_arr?$consume_bonus_arr[0]:[];
        //用户列表
        $where['status']=1;
        $where['type']=$time_types;
        $where['shop_id']=$this->request->shopId;
        if(!empty($search)) $where['search']=$search;
        $order['a.sort']='asc';

        $return = MemberConsumeModel::ulist($where,$order,$page,$limit);

        //入围人数
        $r_where = $where;
        $r_where['partake_status'] = 1;
        $member_num = MemberConsumeModel::where($r_where)->count();

        //无数据直接查设置表
        if($consume_bonus){
            $return['bonus'] = [
                'start_time'=>$consume_bonus?$consume_bonus['start_time']:null,
                'end_time'=>$consume_bonus?$consume_bonus['end_time']:null,
                'bonus_pool'=>$return['list']?$return['list'][0]['all_reward_money']:0,
                'member_num'=>$member_num,
                'time_type'=>$consume_bonus?$consume_bonus['type']:null
            ];
        }else{
            $time = $this->getTime($time_types);
            $return['bonus'] = [
                'start_time'=>$time?$time['start_time']:null,
                'end_time'=>$time?$time['end_time']:null,
                'bonus_pool'=>$return['list']?$return['list'][0]['all_reward_money']:0,
                'member_num'=>$member_num,
                'time_type'=>$time?$time_types:null
            ];
        }

        return $this->success($return);

    }

    /*进行中贡献详情*/
    public function contributeDetail(){
        $type = $this->request->param('type/d',1);//1产品
        $member_id = $this->request->param('member_id/d',0);
        $consume_bonus_id= $this->request->param('consume_bonus_id/d',0);
        if(null === $consume_bonus = ConsumeBonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'is_end'=>0,'consume_bonus_id'=>$consume_bonus_id])->find()){
            return $this->error('分红不存在');
        }
        $order_where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['is_paid','=',1],

            ['member_id','=',$member_id],

            ['pay_time','>=',strtotime($consume_bonus->start_time)],

            ['pay_time','<=',strtotime($consume_bonus->end_time)],
        ];

        $order_ids = GoodsOrderModel::where($order_where)->column('order_id');
        $where = [];
        $item_model = new GoodsOrderItemModel();
        $item_model = $item_model->whereIn('order_id',$order_ids);

        $order=['sort'=>'asc'];
        $field="*";

        $append = ['add_time_format'];

        $return = DataLogic::getDataList($item_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        $list = $return['list'];
        foreach ($list as $k=>$v){
            $flash_sale = FlashSaleGoodsModel::whereTime('start_time','<=',date('Y-m-d'))->whereTime('end_time','>=',date('Y-m-d'))->where(['status'=>1,'goods_id'=>$v['goods_id']])->find();
            $pin_tuan = PinTuanGoodsModel::whereTime('start_time','<=',date('Y-m-d'))->whereTime('end_time','>=',date('Y-m-d'))->where(['status'=>1,'goods_id'=>$v['goods_id']])->find();
            $list[$k]['is_flash_sale'] = $flash_sale?1:0;
            $list[$k]['is_pin_tuan'] = $pin_tuan?1:0;
            $list[$k]['type'] = $type;
        }

        $return['list'] = $list;
        return $this->success($return);
    }

    /*时间类型*/
    public function timeType(){
        $type = $this->request->param('type/d',0);//0进行中1已结束
        $setting = ConsumeBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find();
        if($setting->is_open<1) return $this->success([]);
        if($type<1){
            $status = $type==1?2:1;
            $mc_model = new MemberConsumeModel();
            if(null === $time_types_arr = $mc_model->getTime(['shop_id'=>$this->request->shopId,'status'=>$status],'type')){
                return $this->success([]);
            }
            $time_types = array_column($time_types_arr,'type');
            //如果没数据查设置
            if(!$time_types){

                $time_types = $this->getTimeType($setting);
            }

        }else{
            $time_types = ConsumeBonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->group('time_type')->column('time_type');

        }

        return $this->success($time_types);
    }

    private function getTimeType($consume_bonus_setting){
        $bonus_cycle_type = $consume_bonus_setting->bonus_cycle_type;

        if($consume_bonus_setting->bonus_cycle == 1){
            //单次分红
            if($bonus_cycle_type==1){
                //周
                $time_type[] = 1;

            }else if($bonus_cycle_type==2){
                //月
                $time_type[] = 2;

            }else if($bonus_cycle_type==3){
                //季度
                //获取当前季度
                $time_type[] = 3;

            }else if($bonus_cycle_type==6){
                //当天
                $time_type[] = 6;
            }


        }else {
            //两次分红
            if ($bonus_cycle_type == 1) {
                //周+月
                //本周结束时间
                $time_type []= 1;

                //本月结束时间
                $time_type[] = 2;


            } else if ($bonus_cycle_type == 2) {
                //月+季度
                //本月结束时间
                $time_type[] = 2;

                //本季度结束时间
                $time_type[] = 3;

            } else if ($bonus_cycle_type == 3) {
                //月+半年
                //本月结束时间
                $time_type[] = 2;

                //半年结束时间
                $time_type[] = 4;

            } else if ($bonus_cycle_type == 4) {
                //季度+半年
                //本月结束时间
                $time_type[] = 3;

                //半年结束时间
                $time_type[] = 4;

            } else if ($bonus_cycle_type == 5) {
                //季度+年
                //本月结束时间
                $time_type[] = 3;

                //半年结束时间
                $time_type[] = 5;

            }

        }
        return $time_type;
     }
    private function getTime($time_type){
        $start_time = $end_time = null;
        if($time_type==1){
            //周
            $start_time = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
            $end_time = mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));

        }else if($time_type==2){
            //月
            $start_time = mktime(0,0,0,date('m'),1,date('Y'));
            $end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));

        }else if($time_type==3){
            //季度
            $season = ceil((date('m'))/3);
            //当前季度开始时间戳
            $start_time  = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
            $end_time = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
        }else if($time_type==4){
            //半年
            if (date('m') <= 6) {
                //上半年开始时间
                $start_time = strtotime(date('Y') . '-01-01 00:00:00');
                //上半年结束时间
                $end_time = mktime(23, 59, 59, 6, 1, date('Y'));
            } else {
                //下半年开始时间
                $start_time = strtotime(date('Y') . '-07-01 00:00:00');
                //下半年结束时间
                $end_time = mktime(23, 59, 59, 12, 1, date('Y'));
            }
        }else if($time_type==5){
            //年
            $start_time  = strtotime(date("Y",time())."-1"."-1");
            $end_time  = strtotime(date("Y",time())."-12"."-31 23:59:59");

        }else if($time_type==6){
            //当天
            $start_time = strtotime(date('Y-m-d').'00:00:00');//开始的时间戳
            $end_time = strtotime(date('Y-m-d').'23:59:59');//结束的时间戳
        }
        $return['start_time'] = $start_time?date('Y-m-d H:i:s',$start_time):$start_time;
        $return['end_time'] = $end_time?date('Y-m-d H:i:s',$end_time):$end_time;
        return $return;
    }

    /*已结束*/
    public function endIndex(){
        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];

        $consume_model = new ConsumeBonusModel();

        $order=['add_time'=>'desc'];

        $field="*";

        $append=[];

        $return = DataLogic::getDataList($consume_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);
    }
    /*已结束详情*/
    public function endDetail(){
        $consume_bonus_id = $this->request->param('consume_bonus_id/d',0);
        //消费分红
        if(null === $consume_bonus = ConsumeBonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'consume_bonus_id'=>$consume_bonus_id])->find()){
            return $this->error('分红不存在');
        }
        $where['shop_id'] = $this->request->shopId;
        $where['over_start_time'] = $consume_bonus->start_time;
        $where['over_end_time'] = $consume_bonus->end_time;
        $where['status'] = 2;
        $where['type'] = $consume_bonus->time_type;

        $order=['sort'=>'asc'];

        $return = MemberConsumeModel::ulist($where,$order,$this->request->page,$this->request->limit);

        $return['bonus'] = [
            'start_time'=>$consume_bonus->start_time,
            'end_time'=>$consume_bonus->end_time,
        ];
        return $this->success($return);
    }



}

